﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.BLL.NetBar;
using Jzy.Core.Log.Exceptions;
using FortuneHeaven.Entity.NetBar;
using Jzy.Common;
using Jzy.Core.Utility;

namespace FortuneHeaven.Admin.Pages.NetBar
{
    public partial class RechargeAdd : PageEdit
    {
        private NetBarBO NetBar
        {
            get
            {
                return NetBarBO.Instance<NetBarBO>();
            }
        }

        private RechargeHistoryBO RechargeHistory
        {
            get
            {
                return RechargeHistoryBO.Instance<RechargeHistoryBO>();
            }
        }

        private string NetBarID
        {
            get
            {
                return Request.QueryString["NetBarID"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                NetBar.dbOper.BeginTrans();
                RechargeHistory.dbOper.BeginTrans();

                int amount = int.Parse(txtAmount.Text);


                NetBarInfo info = NetBar.GetInfoWithLock(int.Parse(NetBarID));
                info.Experience += amount;
                info.Score += amount * 500;

                RechargeHistoryInfo historyInfo = new RechargeHistoryInfo();
                historyInfo.Amount = amount;
                historyInfo.Score = info.Score;
                historyInfo.InputTime = DateTime.Now;
                historyInfo.NetBarID = int.Parse(NetBarID);
                historyInfo.NetBarName = info.NetbarName;
                historyInfo.UserID = AppSession.UserID;
                historyInfo.UserName = AppSession.UserName;
                historyInfo.Remark = txtRemark.Text;

                NetBar.Save(info);
                RechargeHistory.Save(historyInfo);



                NetBar.dbOper.CommitTrans();
                RechargeHistory.dbOper.CommitTrans();
                JSUtil.RefreshView();
            }
            catch(Exception ex)
            {
                Alert("充值失败");
                LogExceptionBO.Save(ex);
                NetBar.dbOper.RollbackTrans();
                RechargeHistory.dbOper.RollbackTrans();
            }
        }
    }
}